/**
 * 
 */
package com.sx.article.action;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ModelDriven;
import com.sx.article.biz.IKeyWordBiz;
import com.sx.article.entity.KeyWordsEntity;
import com.sx.core.CoreAction;
import com.sx.feesunit.exception.SXException;

/**
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class KeyWordsAction extends CoreAction implements ModelDriven<KeyWordsEntity> {
	private KeyWordsEntity keyword = new KeyWordsEntity();
	private IKeyWordBiz keywordbiz;
	private Log log = LogFactory.getLog(this.getClass());
	/**
	 *按照用户历史记录查询关键字信息 
	 **/
	public void findKeyWordsHistroyByUserid() throws SXException{
		String str = "{totalCount:0,data:[]}";
		try {
			String queryname = "KeyWords.findHistoryKeyWords";
			str = keywordbiz.findKeyWordsJSONString(keyword,queryname);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("按照用户查询关键字信息",e);
			throw new SXException(e);
		}
		outJsonString(str);
	}
	/**
	 *删除索引关键字 
	 **/
	public void deleteKeyWords() throws SXException{
		try {
			boolean flag = keywordbiz.deleteKeyWords(keyword);
			if (flag) {
				outJsonString("{success:true}");
			} else {
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("删除索引关键字",e);
			throw new SXException(e);
		}
	}
	/**
	 *按照用户查询关键字信息 
	 **/
	public void findKeyWordsByUserid() throws SXException{
		String str = "{totalCount:0,data:[]}";
		try {
			str = keywordbiz.findKeyWordsJSONString(keyword);
		} catch (Exception e) {
			// TODO: handle exception
			log.error("按照用户查询关键字信息",e);
			throw new SXException(e);
		}
		outJsonString(str);
	}
	public void addKeyWords() throws SXException{
		try {
			List<KeyWordsEntity> list = keywordbiz.findKeyWords(keyword, "KeyWords.findKeyWordsByNameOnly");
			if (list.size()!=0) {
				outJsonString("{success:false,ishaveit:true}");
				return;
			}
			Integer id = keywordbiz.addKeyWord(keyword);
			if(id!=null){
				outJsonString("{success:true,id:"+id.intValue()+"}");
			}else{
				outJsonString("{success:false}");
			}
		} catch (Exception e) {
			outJsonString("{success:false}");
			log.error("新增关键字信息",e);
			throw new SXException(e);
			// TODO: handle exception
		}
	}
	/**
	 * @return the keywordbiz
	 */
	public IKeyWordBiz getKeywordbiz() {
		return keywordbiz;
	}

	/**
	 * @param keywordbiz the keywordbiz to set
	 */
	public void setKeywordbiz(IKeyWordBiz keywordbiz) {
		this.keywordbiz = keywordbiz;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */
	public KeyWordsEntity getModel() {
		// TODO Auto-generated method stub
		return keyword;
	}

}
